//
//  moreSpeakViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/4/8.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "moreSpeakViewController.h"
#import "moreSpeakTableViewCell.h"
#import "zhuboModel.h"
#import "SpeakDetailViewController.h"
@interface moreSpeakViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property(nonatomic)BOOL isRefreshing;
@property(nonatomic)BOOL isLoadingMore;
@property(nonatomic)int number;
@property (nonatomic,strong) NSMutableArray *speakArray;
@end

@implementation moreSpeakViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    _speakArray = [[NSMutableArray alloc] init];
    [self navigationItemSetting];
    [self initParamater];
    [self.view addSubview:self.tableView];
    [self createRefreshHeadView];
    [self createRefreshFootView];
    [self fatchData];
    // Do any additional setup after loading the view.
}

- (void)navigationItemSetting{
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"white_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    UIBarButtonItem *rightItem1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed: @"yinle_"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(push)];
    

    
    self.navigationItem.rightBarButtonItems = @[rightItem1];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)push{
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.1];
    [self.navigationController pushViewController:[PlayerViewController sharePlayer] animated:NO];
    [UIView commitAnimations];
}



//刷新
- (void)initParamater{
    self.number = 20;
}

- (void)createRefreshHeadView{
    
    __weak typeof(self) wSelf = self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (wSelf.isRefreshing) {
            return ;
        }

        wSelf.isRefreshing  = YES;
        [wSelf initParamater];
        [wSelf fatchData];
    }];
}

- (void)createRefreshFootView{
    
    __weak typeof(self) wSelf = self;
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (wSelf.isLoadingMore) {
            return ;
        }

        wSelf.isLoadingMore = YES;
        wSelf.number += 10;
        [wSelf fatchData];
    }];
}

- (void)fatchData{
    if (![self fatchFormLocal]) {
        [self fatchFormNetwork];
    }
}

- (BOOL)fatchFormLocal{
    return NO;
}

- (void)fatchFormNetwork{
    //掉落动画
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleDrop];
    [MMProgressHUD showWithTitle:@"努力加载中..."];
    
    NSString * str = [NSString stringWithFormat:SPEAKLISTURL,_number];
    [[NetDataEngine sharedInstance] requestDiscoverUrl:str success:^(id respondObject) {
        if (respondObject) {
            [_speakArray removeAllObjects];
            NSArray *dataArray = respondObject[@"data"];
//            NSLog(@"%@",dataArray);
            for (NSDictionary *diantaiDict in dataArray) {
                zhuboModel *diantaiModel = [[zhuboModel alloc] initWithDictionary:diantaiDict error:nil];
                
                [_speakArray addObject:diantaiModel];
            }
            [MMProgressHUD dismissWithSuccess:@"下载完成" title:@"ok"];
            
            [self endRefreshing];
            [_tableView reloadData];
        }
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
        [self endRefreshing];
    }];

}

- (void)endRefreshing{
    if (self.isRefreshing) {
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
        self.isRefreshing = NO;
    }
    if (self.isLoadingMore) {
        self.isLoadingMore = NO;
        [self.tableView footerEndRefreshing];
    }
}


- (UITableView*)tableView{
    if (_tableView ==  nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49)];
        _tableView.backgroundColor = [UIColor blackColor];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.separatorColor = [UIColor whiteColor];
        
    }
    UINib *nib = [UINib nibWithNibName:@"moreSpeakTableViewCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cellId"];
    return _tableView;
}


#pragma mark -
#pragma mark delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%lu",_speakArray.count);
    return _speakArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    moreSpeakTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
    zhuboModel *model = self.speakArray[indexPath.row];
//    NSLog(@"%@",model);
    [cell.myImageVIew sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed: @"placehold_rec"]];
    cell.titleLabel.text = model.nickname;
    cell.numberLabel.text = [NSString stringWithFormat:@"节目：%@期",model.fmnum];
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    zhuboModel *model = _speakArray[indexPath.row];
    SpeakDetailViewController *speakDetailVC = [[SpeakDetailViewController alloc] init];
    speakDetailVC.title = model.nickname;
    speakDetailVC.userId = model.userId;
    speakDetailVC.nickname = model.nickname;
    speakDetailVC.avatar = model.avatar;
    speakDetailVC.favnum = model.favnum;
    speakDetailVC.viewnum = model.viewnum;
    speakDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:speakDetailVC animated:YES];
}

@end
